﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Drawer
{
    public partial class Setting : Form
    {
        private const int MaxPortNumber = 20;
        public const int MaxStep = 33;
        public const int MinStep = 1;
        public string PortName;
        public int DrawStep = 4;
        private string[] Port = new string[MaxPortNumber];
        public Setting()
        {
            InitializeComponent();
            for (int i = 0; i < MaxPortNumber; i++)
            {
                serialPort1.PortName = "COM" + i.ToString();
                try
                {
                    serialPort1.Open();
                    serialPort1.Close();
                    comboBox1.Items.Add("COM" + i.ToString());
                }
                catch (System.Exception)
                {

                }
            }
            if (comboBox1.Items.Count == 0)
            {
                MessageBox.Show("无可用串口", "错误");
                Close();
            }
            for (int i = 1; i < MaxStep; i++)
                comboBox2.Items.Add(i.ToString());
            comboBox1.SelectedIndex = 0;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void Setting_Load(object sender, EventArgs e)
        {
            if (PortName != null)
            {
                comboBox1.Text = PortName;
                if (comboBox1.Text != PortName)
                    comboBox1.Items.Add(PortName);
                comboBox1.Text = PortName;
            }
            else
            {
                PortName = comboBox1.Text;
            }
            comboBox2.Text = DrawStep.ToString();
        }

        private void Setting_FormClosed(object sender, FormClosedEventArgs e)
        {
            PortName = comboBox1.Text;
            DrawStep = Convert.ToInt16(comboBox2.Text);
        }
    }
}
